/*
 * <<
 *  Davinci
 *  ==
 *  Copyright (C) 2016 - 2019 EDP
 *  ==
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  >>
 *
 */

package com.cwf.metadatamanage.mapper;


import com.cwf.metadatamanage.model.pojo.Source;
import com.cwf.metadatamanage.model.vo.req.SourceReq;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SourceMapper {

    int insert(Source source);

    @Update({"update `t_source` set `status` = 0 where id = #{id,jdbcType=BIGINT}"})
    int deleteById(@Param("id") Long id);

    @Select({"select " +
            " id, name, description, config, type, create_by, create_time, update_by, update_time , organization_id, organization_name, system_id, system_name, create_by_name, update_by_name, register_description, database_type, status " +
            " from `t_source` where id = #{id}"})
    Source getById(@Param("id") Long id);

    @Select({"select " +
            " id, name, description, config, type, create_by, create_time, update_by, update_time , organization_id, organization_name, system_id, system_name, create_by_name, update_by_name, register_description, database_type, status " +
            " from `t_source` where name = #{name}"})
    Source getByName(@Param("name") String name);

    int updateSource(Source source);

    @Select({"select id from `t_source` where project_id = #{projectId} and name = #{name}"})
    Long getByNameWithProjectId(@Param("name") String name, @Param("projectId") Long projectId);

    @Select({"select * from `t_source` where project_id = #{projectId}"})
    List<Source> getByProject(@Param("projectId") Long projectId);

    List<Source> getSourcesList(SourceReq sourceReq);

    @Select({
            "SELECT s.id, s.`name`, s.`type`, s.`config`,",
            "	p.id 'project.id',",
            "	p.`name` 'project.name',",
            "	p.`description` 'project.description',",
            "	p.`pic` 'project.pic',",
            "	p.`user_id` 'project.userId',",
            "	p.`org_id` 'project.orgId',",
            "	p.`visibility` 'p.visibility'",
            "FROM t_source s INNER JOIN project p on p.id = s.project_id",
            "where s.id = #{sourceId}"
    })
//    SourceWithProject getSourceWithProjectById(@Param("sourceId") Long sourceId);

//    int insertBatch(@Param("list") List<Source> sourceList);

    @Delete({"delete from `t_source` where project_id = #{projectId}"})
    int deleteByProject(@Param("projectId") Long projectId);
}